package com.example.mydemo.service;

import cn.dev33.satoken.stp.StpInterface;
import com.example.mydemo.entity.domain.MenuRoleVO;
import com.example.mydemo.entity.domain.UserRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限认证接口扩展，Sa-Token 将从此实现类获取每个账号拥有的权限码
 *
 * @author kong
 * @since 2022-10-13
 */
@Service
@Component    // 打开此注解，保证此类被springboot扫描，即可完成sa-token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    private IMenuRoleService IMenuRoleService;
    @Autowired
    private IUserRoleService IUserRoleService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<MenuRoleVO> a = IMenuRoleService.selectByRoleId(Long.parseLong((String) loginId) );
        List<String> permission= a.stream().map(MenuRoleVO::getPermission).collect(Collectors.toList());
        return permission;
    }

    /**
     * 返回一个账号所拥有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<UserRoleVO> a = IUserRoleService.selectByUserName((String) loginId);
        List<String> roles= a.stream().map(UserRoleVO::getRoleName).collect(Collectors.toList());
        return roles;
    }

}

